package com.totcy.atom.dialog;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.totcy.atom.R;
import com.totcy.atom.adapter.SubjectListAdapter;
import com.totcy.atom.bean.SubjectBean;
import com.totcy.baselibrary.adapter.BaseRecyleAdapter;
import com.totcy.baselibrary.adapter.divider.RecycleViewDivider;

import java.util.List;

public class SubjectDialog implements BaseRecyleAdapter.OnItemClickListener {
    private AlertDialog alertDialog;
    private List<SubjectBean> subjects;
    private SubjectListAdapter subjectListAdapter;

    public SubjectDialog(Activity activity, List<SubjectBean> subjects, OnSubjectClick onSubjectClick) {
        this.onSubjectClick = onSubjectClick;
        this.subjects = subjects;

        View contentView = activity.getLayoutInflater().inflate(R.layout.dialog_subject, null);
        RecyclerView recyclerView = contentView.findViewById(R.id.rv_subject);

        LinearLayoutManager lm = new LinearLayoutManager(activity);
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(lm);

        recyclerView.addItemDecoration(new RecycleViewDivider(
                activity, LinearLayoutManager.VERTICAL, (int) (activity.getResources().getDisplayMetrics().density * 0.5),
                ContextCompat.getColor(activity, R.color.cut_line_color))
                .setDrawTop(false));

        subjectListAdapter = new SubjectListAdapter(activity, this.subjects);
        recyclerView.setAdapter(subjectListAdapter);
        subjectListAdapter.setOnItemClickListener(this);
        alertDialog = new AlertDialog.Builder(activity)
                .setView(contentView)
                .create();

    }

    public void showDialog() {
        if (alertDialog != null) {
            alertDialog.show();
        }
    }

    private OnSubjectClick onSubjectClick;

    @Override
    public void onItemClick(View view, int position) {
        if (onSubjectClick != null) {
            onSubjectClick.onItemType(subjects.get(position));
        }
        alertDialog.dismiss();
    }

    public void initDate() {
        subjectListAdapter.notifyDataSetChanged();
    }

    public interface OnSubjectClick {
        void onItemType(SubjectBean subjectBean);
    }
}
